<template><div><h2 id="http-客户端" tabindex="-1"><a class="header-anchor" href="#http-客户端"><span>HTTP 客户端</span></a></h2>
<p>Yii2 框架的官方扩展 <code v-pre>yii2-httpclient</code> 为我们提供了 HTTP 客户端相关功能。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>安装此扩展的首选方式是通过 <a href="http://getcomposer.org/download/" target="_blank" rel="noopener noreferrer">composer</a>，命令如下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require yiisoft/yii2-httpclient</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者在您的 <code v-pre>composer.json</code> 文件中的 require 部分增加以下内容：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token property">"yiisoft/yii2-httpclient"</span><span class="token operator">:</span> <span class="token string">"~2.0.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后执行 <code v-pre>composer install</code> 命令即可。</p>
<h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<p>为了发送 HTTP 请求，你需要实例化 <code v-pre>\yii\httpclient\Client</code> 并使用它的 <code v-pre>createRequest()</code> 方法来创建一个新的 HTTP 请求。然后您应该根据您的目标，配置所有的请求参数并发送请求。最后，你会得到一个 <code v-pre>yiii\httpclient\Response</code> 实例，它保存了所有的响应信息和数据。例如:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/api/1.0/users'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token property">isOk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$newUserId</span> <span class="token operator">=</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>get()</code>、<code v-pre>post()</code>、<code v-pre>put()</code> 等快捷方法来简化新请求的准备工作。如果你使用单一的 <code v-pre>yii\httpclient\Client</code> 实例对同一个域进行多个请求（例如使用 REST API 的情况），你可以使用这个域设置它的 <code v-pre>baseUrl</code> 属性。这将允许您在创建新请求时只指定相对 URL。因此，对某些 REST API 的请求，可能如下所示:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/api/1.0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$newUserResponse</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$articleResponse</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'articles'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii 2.0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$client</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'subscriptions'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user_id'</span> <span class="token operator">=></span> <span class="token variable">$newUserResponse</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'article_id'</span> <span class="token operator">=></span> <span class="token variable">$articleResponse</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用不同的内容格式" tabindex="-1"><a class="header-anchor" href="#使用不同的内容格式"><span>使用不同的内容格式</span></a></h3>
<p>默认情况下，HTTP 请求数据以 <code v-pre>form-urlencoded</code> 的方式发送，例如 <code v-pre>param1=value1&amp;param2=value2</code>。这是 web 表单的一种常见格式，但不是针对 REST API，后者通常要求内容应该是 JSON 或 XML 格式。你可以使用 <code v-pre>format</code> 属性或 <code v-pre>setFormat()</code> 方法，来设置请求内容使用的格式。主要支持以下格式：</p>
<ul>
<li><code v-pre>\yii\httpclient\Client::FORMAT_JSON</code> - JSON 格式</li>
<li><code v-pre>\yii\httpclient\Client::FORMAT_URLENCODED</code> - urlencoded（RFC1738 查询字符串）</li>
<li><code v-pre>\yii\httpclient\Client::FORMAT_RAW_URLENCODED</code> - urlencoded（PHP_QUERY_RFC3986 查询字符串）</li>
<li><code v-pre>\yii\httpclient\Client::FORMAT_XML</code> - XML 格式</li>
</ul>
<p>例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/api/1.0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setFormat</span><span class="token punctuation">(</span><span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">FORMAT_JSON</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'articles/search'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'query_string'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'filter'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'date'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'>'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2015-08-01'</span><span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>响应对象根据 <code v-pre>Content-Type</code> 头和内容本身，来自动检测内容格式。所以，在大多数情况下，你不需要为响应指定格式，你可以简单地使用 <code v-pre>getData()</code> 方法或 <code v-pre>data</code> 属性来解析它。继续上面的例子，我们可以通过以下方式得到响应数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$responseData</span> <span class="token operator">=</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// get all articles</span></span>
<span class="line"><span class="token function">count</span><span class="token punctuation">(</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">)</span> <span class="token comment">// count articles</span></span>
<span class="line"><span class="token variable">$article</span> <span class="token operator">=</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment">// get first article</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用原始内容" tabindex="-1"><a class="header-anchor" href="#使用原始内容"><span>使用原始内容</span></a></h3>
<p>内置的格式，并非是要强制性使用的。您可以为 HTTP 请求指定原始内容，同样也可以处理响应的原始内容。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/api/1.0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'articles/search'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'content-type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/json'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setContent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{query_string: "Yii"}'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token string single-quoted-string">'Search results:&lt;br>'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>\yii\httpclient\Request</code> 格式仅在 <code v-pre>content</code> 未设置时指定 <code v-pre>data</code>。<code v-pre>\yii\httpclient\Response</code> 仅在 <code v-pre>data</code> 被请求时，解析 <code v-pre>content</code>。</p>
<h3 id="预配置请求与响应对象" tabindex="-1"><a class="header-anchor" href="#预配置请求与响应对象"><span>预配置请求与响应对象</span></a></h3>
<p>如果你使用 <code v-pre>\yiii\httpclient\Client</code> 的单个实例来处理一些类似的请求，例如，在使用 REST API时，你可以简化并加快声明自己的请求和响应对象配置的代码开发速度。这可以通过 <code v-pre>\yii\httpclient\Client</code> 的 <code v-pre>requestConfig</code> 和 <code v-pre>responseConfig</code> 字段来完成。你可能想设置 JSON 格式的所有由特定的客户端创建的请求，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/api/1.0'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'requestConfig'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'format'</span> <span class="token operator">=></span> <span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">FORMAT_JSON</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'responseConfig'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'format'</span> <span class="token operator">=></span> <span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">FORMAT_JSON</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">format</span><span class="token punctuation">;</span> <span class="token comment">// outputs: 'json'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：您甚至可以在配置数组中使用 ‘class’ 键，为请求和响应对象指定你自己的类，来促进你需要的一些额外的功能。</p>
</blockquote>
<h3 id="使用请求头" tabindex="-1"><a class="header-anchor" href="#使用请求头"><span>使用请求头</span></a></h3>
<p>您可以使用 <code v-pre>setHeaders()</code> 和 <code v-pre>addHeaders()</code> 方法来指定请求头。你也可以使用 <code v-pre>getHeaders()</code> 方法 or <code v-pre>headers</code> 属性来获取已经定义的作为 <code v-pre>\yii\web\HeaderCollection</code> 实例的头。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/api/1.0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'content-type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/json'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user-agent'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'My User Agent'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'accept-language'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'en-US;en'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">headers</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user-agent'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'User agent override'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你有了一个响应对象，你可以使用 <code v-pre>getHeaders()</code> 方法或 <code v-pre>headers</code> 属性来访问所有的响应头，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'content-type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">headers</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'content-encoding'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="http-基本认证" tabindex="-1"><a class="header-anchor" href="#http-基本认证"><span>HTTP 基本认证</span></a></h4>
<p>本客户端没有内置对 HTTP 身份验证的支持。如果你想使用 HTTP 基本认证，你需要添加一个符合 <a href="https://tools.ietf.org/html/rfc2617#section-2" target="_blank" rel="noopener noreferrer">HTTP 基本验证格式</a> 的合适头信息。你可以添加一个 <code v-pre>Authorization</code> 头，它包含单词 <code v-pre>Basic</code>，后跟一个空格和 base64 编码的字符串 <code v-pre>username:password</code>，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$username</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'yii'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'verysecret'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">headers</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Authorization'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Basic '</span> <span class="token operator">.</span> <span class="token function">base64_encode</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"<span class="token interpolation"><span class="token variable">$username</span></span>:<span class="token interpolation"><span class="token variable">$password</span></span>"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：HTTP 基本认证在明文中发送账号和密码，因此，它应该只在使用 HTTPS 的安全连接上使用。</p>
</blockquote>
<h3 id="使用-cookies" tabindex="-1"><a class="header-anchor" href="#使用-cookies"><span>使用 cookies</span></a></h3>
<p>虽然 Cookies 是像报头值一样传输的，<code v-pre>\yii\httpclient\Request</code> 和 <code v-pre>\yii\httpclient\Request</code> 仍然提供了单独的接口来使用 <code v-pre>\yii\web\Cookie</code> 和 <code v-pre>\yii\web\CookieCollection</code>。</p>
<p>你可以使用 <code v-pre>setCookies()</code> 或 <code v-pre>addCookies()</code> 方法来指定请求的 cookie。你可以使用 <code v-pre>getCookies()</code> 方法或 <code v-pre>cookies</code> 属性来获取已经定义的 cookie ，并作为 <code v-pre>yiii \web\CookieCollection</code> 类的实例。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Cookie</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/api/1.0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setCookies</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'country'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'USA'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">new</span> <span class="token class-name">Cookie</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'language'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en-US'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addCookies</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'view_mode'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'full'</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">cookies</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'display-notification'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦当你有了一个响应对象，你可以使用 <code v-pre>getCookies()</code> 方法或 <code v-pre>cookies</code> 属性访问所有的响应的 cookie，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">getCookies</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'country'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">headers</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'PHPSESSID'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以使用简单的复制，将 cookie 从响应对象转移到请求。例如，假设我们需要在一些 web 应用程序中编辑用户配置文件，它只有在登录后才可用，所以，我们需要首先登录并使用创建的会话进行进一步的活动，代码如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$loginResponse</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'somepassword'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $loginResponse->cookies->get('PHPSESSID') - holds the new session ID</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'birthDate'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'10/11/1982'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setCookies</span><span class="token punctuation">(</span><span class="token variable">$loginResponse</span><span class="token operator">-></span><span class="token property">cookies</span><span class="token punctuation">)</span> <span class="token comment">// transfer response cookies to request</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用-curl-下载文件" tabindex="-1"><a class="header-anchor" href="#使用-curl-下载文件"><span>使用 Curl 下载文件</span></a></h3>
<p>您可以下载一个文件，而无需将其全部内容加载到内存中（这一点，对于大文件特别有用）。使用 <code v-pre>setOutputFile()</code> 方法将流资源（例如使用 <code v-pre>fopen()</code>）传递给请求对象。它将被传递到 <code v-pre>CURLOPT_FILE</code> cURL选项。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$fh</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/local/file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'w'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\httpclient\CurlTransport'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GET'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/path/to/remote/file'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setOutputFile</span><span class="token punctuation">(</span><span class="token variable">$fh</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用-curl-上传文件" tabindex="-1"><a class="header-anchor" href="#使用-curl-上传文件"><span>使用 Curl 上传文件</span></a></h3>
<p>你可以使用 <code v-pre>CURLFile</code> （PHP &gt;= 5.5.0）上传一个文件，而不需要将其全部内容加载到内存中（这一点，对于大文件特别有用）。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\httpclient\CurlTransport'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setFormat</span><span class="token punctuation">(</span><span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">FORMAT_CURL</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'file1'</span> <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>CURLFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'text/plain'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'file1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'file2'</span> <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>CURLFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file2'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'text/plain'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'file2'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据格式" tabindex="-1"><a class="header-anchor" href="#数据格式"><span>数据格式</span></a></h2>
<p>数据格式决定了组成或解析 HTTP 消息内容的方式，例如，它决定了 <code v-pre>\yii\httpclient\Message::$data</code> 应该如何转换为 <code v-pre>\yii\httpclient\Message::$content</code>，反之亦然。默认支持以下格式：</p>
<ul>
<li><code v-pre>\yii\httpclient\Client::FORMAT_JSON</code> - JSON 格式</li>
<li><code v-pre>\yii\httpclient\Client::FORMAT_URLENCODED</code> - urlencoded（RFC1738 查询字符串）</li>
<li><code v-pre>\yii\httpclient\Client::FORMAT_RAW_URLENCODED</code> - urlencoded（PHP_QUERY_RFC3986 查询字符串）</li>
<li><code v-pre>\yii\httpclient\Client::FORMAT_XML</code> - XML 格式</li>
</ul>
<p>每种格式都有两个实体：<code v-pre>formatter（格式器）</code> 和 <code v-pre>parser（解析器）</code>。前者决定由数据组成请求内容的方式，后者决定如何将原始响应内容解析为数据。</p>
<p><code v-pre>\yii\httpclient\Client</code> 自动为上述所有格式选择相应的格式器和解析器。但是你可以使用 <code v-pre>\yii\httpclient\Client::$formatters</code> 和 <code v-pre>\yii\httpclient\Client::$parser</code> 来改变这个行为。使用这些字段，您可以添加自己的格式或更改标准格式。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'formatters'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'myformat'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\http\MyFormatter'</span><span class="token punctuation">,</span> <span class="token comment">// add new formatter</span></span>
<span class="line">        <span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">FORMAT_XML</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\http\MyXMLFormatter'</span><span class="token punctuation">,</span> <span class="token comment">// override default XML formatter</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'parsers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// configure options of the JsonParser, parse JSON as objects</span></span>
<span class="line">        <span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">FORMAT_JSON</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\httpclient\JsonParser'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'asArray'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在创建你自己的解析器时，你应该实现 <code v-pre>\yii\httpclient\ParserInterface</code>，同时，创建格式化器需要实现 <code v-pre>\yii\httpclient\FormatterInterface</code>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>FormatterInterface</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>ParserInterface</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Response</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ParserIni</span> <span class="token keyword">implements</span> <span class="token class-name">ParserInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">parse</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Response</span> <span class="token variable">$response</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">parse_ini_string</span><span class="token punctuation">(</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">FormatterIni</span> <span class="token keyword">implements</span> <span class="token class-name">FormatterInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">format</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'text/ini; charset=UTF-8'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$pairs</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">data</span> <span class="token keyword">as</span> <span class="token variable">$name</span> <span class="token operator">=></span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$pairs</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string double-quoted-string">"<span class="token interpolation"><span class="token variable">$name</span></span>=<span class="token interpolation"><span class="token variable">$value</span></span>"</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">setContent</span><span class="token punctuation">(</span><span class="token function">implode</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"\n"</span><span class="token punctuation">,</span> <span class="token variable">$pairs</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="传输" tabindex="-1"><a class="header-anchor" href="#传输"><span>传输</span></a></h2>
<p><code v-pre>\yii\httpclient\Client</code> 提供了几种不同的方式来实际发送 HTTP 消息，即几种预定义传输方式。具体如下：</p>
<ul>
<li><code v-pre>\yii\httpclient\StreamTransport</code>：使用 <a href="http://php.net/manual/zh/book.stream.php" target="_blank" rel="noopener noreferrer">Streams</a> 发送 HTTP 消息。默认情况下使用此传输，它不需要安装任何额外的 PHP 扩展或库，但不支持像批量发送这样的高级特性。</li>
<li><code v-pre>\yii\httpclient\CurlTransport</code>：使用 <a href="http://php.net/manual/zh/book.curl.php" target="_blank" rel="noopener noreferrer">cURL（Client URL 库）</a> 发送 HTTP 消息。这种传输需要安装 PHP ‘curl’ 扩展，但提供了对高级功能的支持，如批量发送。</li>
<li><code v-pre>\yii\httpclient\MockTransport</code>：主要在测试自动化环境中使用。它不发送任何真正的请求，而是返回指示它返回的响应。</li>
</ul>
<p>您可以使用 <code v-pre>\yii\httpclient\Client::$transport</code> 属性将传输配置为特定的客户端，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\httpclient\CurlTransport'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义传输" tabindex="-1"><a class="header-anchor" href="#自定义传输"><span>自定义传输</span></a></h3>
<p>您可以创建自己的传输，它将以自己的方式执行消息发送。要做到这一点，你应该继承 <code v-pre>yiii \httpclient\Transport</code> 类，并至少实现 <code v-pre>send()</code> 方法。所有你需要做的就是确定 HTTP 响应内容和报头，然后你可以用 <code v-pre>yii\httpclient\Client::createResponse()</code>，为它们组成一个响应对象。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Transport</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MyTransport</span> <span class="token keyword">extends</span> <span class="token class-name">Transport</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">send</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$responseContent</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'???'</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$responseHeaders</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'???'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">client</span><span class="token operator">-></span><span class="token function">createResponse</span><span class="token punctuation">(</span><span class="token variable">$responseContent</span><span class="token punctuation">,</span> <span class="token variable">$responseHeaders</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果有一个方式发送多个请求具有更好的性能，例如，异步地并行发送它们，你也可以重写 <code v-pre>batchSend()</code> 方法。</p>
<h2 id="请求选项" tabindex="-1"><a class="header-anchor" href="#请求选项"><span>请求选项</span></a></h2>
<p>你可以使用 <code v-pre>\yii\httpclient\Request::$options</code> 来调整特定请求的执行。我们主要支持以下选项，但区分大小写：</p>
<ul>
<li><code v-pre>timeout</code>：integer，允许执行请求的最大秒数。</li>
<li><code v-pre>proxy</code>：string，指定代理服务器地址的 URI（例如：tcp://proxy.example.com:5100）。</li>
<li><code v-pre>userAgent</code>：string，在 HTTP 请求中使用的 <code v-pre>User-Agent:</code> 头的内容。</li>
<li><code v-pre>followLocation</code>：boolean，是否遵循服务器作为 HTTP 报头的一部分发送的任何 <code v-pre>Location:</code> 报头。</li>
<li><code v-pre>maxRedirects</code>：integer，要跟随的最大重定向数。</li>
<li><code v-pre>sslVerifyPeer</code>：boolean，是否验证对端证书。</li>
<li><code v-pre>sslCafile</code>：string，证书颁发机构文件在本地文件系统上的位置，应该与 ‘sslVerifyPeer’选项一起使用，以验证远程对等体的身份。</li>
<li><code v-pre>sslCapath</code>：string，存放多个CA证书的目录。</li>
<li><code v-pre>sslLocalCert</code>：文件系统上本地证书文件的路径。它必须是一个 PEM 编码的文件，包含您的证书和私钥。它可以选择性地包含颁发者的证书链。私钥也可以包含在由 sslLocalPk 指定的单独文件中。</li>
<li><code v-pre>sslLocalPk</code>：在证书（sslLocalCert）和私钥文件是单独的文件的情况下的文件系统上本地私钥文件的路径。</li>
<li><code v-pre>sslPassphrase</code>：用于对 sslLocalCert 文件进行编码的密码短语。</li>
</ul>
<p>例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/api/1.0/users'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe@domain.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setOptions</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'proxy'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tcp://proxy.example.com:5100'</span><span class="token punctuation">,</span> <span class="token comment">// use a Proxy</span></span>
<span class="line">        <span class="token string single-quoted-string">'timeout'</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token comment">// set timeout to 5 seconds for the case server is not responding</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：你可以通过 <code v-pre>yii\httpclient\Client::$requestConfig</code> 设置默认的请求选项。如果你这样做了，使用 <code v-pre>yii\httpclient\Request::addOptions()</code> 来保留它们的值，如果你希望为请求添加额外的特定选项。</p>
</blockquote>
<p>您也可以传递选项，这些选项对于特定的请求传输是特定的。通常在使用 <code v-pre>yiii \httpclient\CurlTransport</code> 的情况下会出现这种情况。例如：您可能希望为连接和接收数据指定分离的超时，PHP cURL 库支持这种超时。你可以这样做：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\httpclient\CurlTransport'</span> <span class="token comment">// only cURL supports the options we need</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/api/1.0/users'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe@domain.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setOptions</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">CURLOPT_CONNECTTIMEOUT</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token comment">// connection timeout</span></span>
<span class="line">        <span class="token constant">CURLOPT_TIMEOUT</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token comment">// data receiving timeout</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关特定选项支持的详细信息，请参阅特定的传输类文档。</p>
<h2 id="multi-part-内容-multipart-form-data" tabindex="-1"><a class="header-anchor" href="#multi-part-内容-multipart-form-data"><span>Multi-part 内容（multipart/form-data）</span></a></h2>
<p>HTTP 消息内容可能由不同内容类型的几个部分组成。这在文件上传请求的情况下通常是必要的。你可以使用 <code v-pre>yii\httpclient\Request</code> 的 <code v-pre>addContent()</code>、<code v-pre>addFile()</code> 或 <code v-pre>addFileContent()</code> 方法来组合多部分内容。例如，如果您希望通过 web 表单模拟文件上传，您可以使用如下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/file/upload'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/source/file.jpg'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果有 <code v-pre>\yii\httpclient\Request::$data</code> 指定，它的值将自动作为内容部分发送，以防请求被标记为多部分之一。例如，假设我们希望模拟表单的提交，具体如下：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>profile-form<span class="token punctuation">"</span></span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post<span class="token punctuation">"</span></span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://domain.com/user/profile<span class="token punctuation">"</span></span> <span class="token attr-name">enctype</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>multipart/form-data<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>username<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>email<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>file<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>avatar<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token comment">&lt;!-- ... --></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用下面的代码来做到这一点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/user/profile'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'johndoe@domain.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/source/image.jpg'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请注意，附加多个具有相同名称的文件将导致它们被最新的文件覆盖。您必须自己控制附加文件的可能的表格输入索引。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/gallery'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar[0]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/source/image1.jpg'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar[1]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/source/image2.jpg'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="批处理请求发送" tabindex="-1"><a class="header-anchor" href="#批处理请求发送"><span>批处理请求发送</span></a></h2>
<p>HTTP 客户端允许你使用 <code v-pre>\yii\httpclient\Client::batchSend()</code> 方法，一次发送多个请求，代码如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$requests</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/keep-alive'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/notify'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'userId'</span> <span class="token operator">=></span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">batchSend</span><span class="token punctuation">(</span><span class="token variable">$requests</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>特定的传输可以从这种方法的使用中受益，从而提高性能。在内置的传输中，只有<code v-pre>yii\httpclient\CurlTransport</code> 这样做。它允许并行发送请求，这节省了程序执行时间。</p>
<blockquote>
<p>Note：只有一些特殊的传输允许以特殊的方式处理 <code v-pre>batchSend()</code> 上的请求，这提供了一些好处。默认情况下，传输只是一个接一个地发送它们，而不会抛出任何错误或警告。如果希望实现性能提升，请确保为客户机配置了正确的传输。</p>
</blockquote>
<p><code v-pre>batchSend()</code> 方法返回响应数组，这些键对应于请求数组中的键。这允许你以简单的方式处理特定的请求响应，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$requests</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'news'</span> <span class="token operator">=></span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/news'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'friends'</span> <span class="token operator">=></span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/user/friends'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'userId'</span> <span class="token operator">=></span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'newComment'</span> <span class="token operator">=></span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://domain.com/user/comments'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'userId'</span> <span class="token operator">=></span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'content'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'New comment'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">batchSend</span><span class="token punctuation">(</span><span class="token variable">$requests</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// result of `GET http://domain.com/news` :</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">isOk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// result of `GET http://domain.com/user/friends` :</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'friends'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">isOk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'friends'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// result of `POST http://domain.com/user/comments` :</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'newComment'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token property">isOk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token string double-quoted-string">"Comment has been added successfully"</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p><code v-pre>\yii\httpclient\Request</code> 提供了几个事件，可以通过事件处理程序或行为来处理：</p>
<ul>
<li><code v-pre>\yii\httpclient\Request::EVENT_BEFORE_SEND</code>：在发送请求之前被引发</li>
<li><code v-pre>\yii\httpclient\Request::EVENT_AFTER_SEND</code>：发送请求后引发</li>
</ul>
<p>这些事件可以用来调整请求参数或接收到的响应。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>RequestEvent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token variable">$client</span><span class="token operator">-></span><span class="token function">createRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GET'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://api.domain.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'param'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Ensure signature generation based on final data set:</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">EVENT_BEFORE_SEND</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">RequestEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">request</span><span class="token operator">-></span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token function">md5</span><span class="token punctuation">(</span><span class="token function">http_build_query</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'signature'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$signature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">request</span><span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Normalize response data:</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">EVENT_AFTER_SEND</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">RequestEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">response</span><span class="token operator">-></span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'content'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">base64_decode</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'encoded_content'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">response</span><span class="token operator">-></span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将事件处理程序附加到 <code v-pre>yiii \httpclient\Request</code> 实例是不太实际的。你可以使用 <code v-pre>\yii\httpclient\Client</code> 类的事件来处理相同的用例，例如：</p>
<ul>
<li><code v-pre>\yii\httpclient\Client::EVENT_BEFORE_SEND</code>：在发送请求之前被引发</li>
<li><code v-pre>\yii\httpclient\Client::EVENT_AFTER_SEND</code>：发送请求后引发</li>
</ul>
<p>这些事件会在所有通过客户端创建的请求中以相同的方式被触发，并且具有与 <code v-pre>\yii\httpclient\Request</code> 相同的签名。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>RequestEvent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$client</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">EVENT_BEFORE_SEND</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">RequestEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$client</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token constant">EVENT_AFTER_SEND</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">RequestEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note：<code v-pre>\yii\httpclient\Client</code> 和 <code v-pre>\yii\httpclient\Request</code> 共享 <code v-pre>EVENT_BEFORE_SEND</code> 和 <code v-pre>EVENT_AFTER_SEND</code> 事件的相同名称，所以你可以创建可以应用于这两个类的行为。</p>
</blockquote>
<h2 id="日志与分析" tabindex="-1"><a class="header-anchor" href="#日志与分析"><span>日志与分析</span></a></h2>
<p>这个扩展允许记录 HTTP 请求被发送的日志和分析他们的执行情况。为了设置一个日志目标，它可以捕获所有与 HTTP 请求相关的条目，你应该使用类目 <code v-pre>yii\httpclient\Transport*</code>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'log'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">            <span class="token string single-quoted-string">'targets'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\log\FileTarget'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'logFile'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@runtime/logs/http-request.log'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'categories'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'yii\httpclient\*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token comment">// ...</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您也可以使用 HTTP 客户端调试面板去查看所有关联的日志，在最后的章节会讲到。</p>
<blockquote>
<p>Attention：由于一些 HTTP 请求的内容可能非常长，将其完整保存在日志中可能会导致某些问题。因此，对请求内容的最大长度有限制，这些内容将被放置在日志中。这是通过 <code v-pre>\yii\httpclient\Client::$contentLoggingMaxSize</code> 属性控制的。任何超出的内容将在日志记录之前被裁剪。</p>
</blockquote>
<h2 id="设置客户端实例" tabindex="-1"><a class="header-anchor" href="#设置客户端实例"><span>设置客户端实例</span></a></h2>
<p>我们可以从实例化 <code v-pre>yii\httpclient\Client</code> 对象开始，使用这个扩展。有几种方法可以将 <code v-pre>yiii\httpclient\Client</code> 集成到你的程序中。这里描述了最常见的方法。</p>
<h3 id="作为应用组件" tabindex="-1"><a class="header-anchor" href="#作为应用组件"><span>作为应用组件</span></a></h3>
<p><code v-pre>\yii\httpclient\Client</code> 继承 <code v-pre>\yii\base\Component</code>，因此，它可以在 <code v-pre>\yii\di\Container</code> 级别设置，作为模块或应用程序组件。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'phpNetHttp'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\httpclient\Client'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://uk.php.net'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">phpNetHttp</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'docs.php'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="扩展客户端类" tabindex="-1"><a class="header-anchor" href="#扩展客户端类"><span>扩展客户端类</span></a></h3>
<p>自从 <code v-pre>\yii\httpclient\Client</code> 可以用作应用程序组件，你就可以扩展它，添加一些你需要的自定义逻辑。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MyRestApi</span> <span class="token keyword">extends</span> <span class="token class-name">Client</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$baseUrl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'http://my.rest.api/'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">addUser</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token property">isOk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>Exception</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Unable to add user.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="包装客户端对象" tabindex="-1"><a class="header-anchor" href="#包装客户端对象"><span>包装客户端对象</span></a></h3>
<p>你可以使用 <code v-pre>\yii\httpclient\Client</code> 实例作为组件的内部字段，它提供了一些复杂的功能。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Component</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>httpclient<span class="token punctuation">\</span>Client</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MyRestApi</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$baseUrl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'http://my.rest.api/'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">private</span> <span class="token variable">$_httpClient</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getHttpClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">is_object</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">_httpClient</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">_httpClient</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">createObject</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name static-context">Client</span><span class="token operator">::</span><span class="token function">className</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">baseUrl</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">_httpClient</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">addUser</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">getHttpClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token property">isOk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>Exception</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Unable to add user.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用-http-客户端调试面板" tabindex="-1"><a class="header-anchor" href="#使用-http-客户端调试面板"><span>使用 HTTP 客户端调试面板</span></a></h2>
<p>Yii 2 HTTP 客户端扩展提供了一个调试面板，可以与 Yii 2 调试模块集成，显示执行的 HTTP 请求。</p>
<p>在你的应用程序配置中添加以下内容来启用它，如果你已经启用了调试模块，只需要添加面板配置就足够了。具体如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'debug'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'modules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'debug'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\\debug\\Module'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'panels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'httpclient'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\\httpclient\\debug\\HttpClientPanel'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此面板允许您执行已记录的 HTTP 请求以查看其响应。您可以以文本形式获得响应，也可以直接将其传递给浏览器。</p>
<blockquote>
<p>Note：只有常规记录的HTTP请求可以通过调试面板执行，批量发送的请求不能。还要记住，日志请求的内容可以根据 <code v-pre>yii\httpclient\Client::$contentLoggingMaxSize</code> 属性进行修剪，所以它的执行可能会失败或产生意想不到的结果。</p>
</blockquote>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


